package com.qst.survey.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qst.survey.entity.Replay;
import com.qst.survey.entity.UserReply;
import com.qst.survey.pojo.PageResult;
import com.qst.survey.pojo.ReplayData;
import com.qst.survey.pojo.Result;
import com.qst.survey.pojo.StatusCode;
import com.qst.survey.service.*;
import com.qst.survey.vo.VM;
import com.qst.survey.vo.ViewDataVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description: 回答投票控制类
 */
@RestController
@RequestMapping("/answer")
@CrossOrigin
public class AnswerController {
    @Autowired
    AnswerService answerService;
    @Autowired
    ReplayService replayService;
    @Autowired
    QuestionService questionService;
    @Autowired
    UserReplyService userReplyService;
    @Autowired
    UserService userService;

    /**
     * @param replayData
     * @return com.qst.survey.pojo.Result
     * @description TODO   提交投票检查是否答题完成
     * @author IsAriel~~
     * @date 2021/4/2
     */
    @PostMapping("/replay")
    public Result replay(@RequestBody ReplayData replayData) {
        System.out.println(replayData);
        if (replayData.getTextId().size() != replayData.getType0().size()) {
            return new Result(false, StatusCode.ERROR, "提交失败!答题不完整!");
        }
        UserReply userReply = new UserReply();
        if (replayData.getUserId() == null) {
            replayData.setUserId(0);
        }
        userReply.setUserId(replayData.getUserId());
        userReply.setQuestionId(replayData.getQuestionId());

        userReplyService.addReply(userReply);
        questionService.addCount(replayData.getQuestionId());
        answerService.chooseAnswerList(replayData.getType1());
        answerService.chooseAnswerList(replayData.getType2());
        replayService.insertReplayList(replayData.getTextId(), replayData.getType0(), replayData.getUserId());

        return new Result(true, StatusCode.OK, "提交成功!");
    }

    /**
     * @param current
     * @param size
     * @param topicId
     * @return com.qst.survey.pojo.Result
     * @description TODO 分页查询回答的数据页面
     * @author IsAriel~~
     * @date 2021/4/2
     */
    @GetMapping("/replayPage/{size}/{topicId}/{current}")
    public Result findReplayPageBYId(@PathVariable Integer current, @PathVariable Integer size, @PathVariable Integer topicId) {
        Page page = new Page<Replay>(current, size);
        page = replayService.findPageById(page, topicId);
        PageResult pageResult = new PageResult(page.getTotal(), page.getRecords());
        return new Result(true, StatusCode.OK, "查询成功!", pageResult);
    }

    @GetMapping("/viewData/{id}")
    public Result dataViewData(@PathVariable Integer id) {
        ViewDataVo viewDataVo = new ViewDataVo();
        viewDataVo.setVmList1(new ArrayList<>());
        viewDataVo.setLengthDataList(new ArrayList<>());
        viewDataVo.getLengthDataList().add("男");
        viewDataVo.getLengthDataList().add("女");
        List<VM> sex = userService.findSex(id);
        viewDataVo.setVmList1(sex);
        List<VM> age = userService.findAge(id);
        for (int i = 0; i < age.size(); i++) {
            viewDataVo.getLengthDataList().add(age.get(i).getName());
        }
        viewDataVo.setVmList2(age);
        return new Result(true, StatusCode.OK, "查询成功!", viewDataVo);
    }

}
